package com.vipheyue.bigbusiness.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.util.LogUtils;
import com.vipheyue.bigbusiness.R;
import com.vipheyue.bigbusiness.bean.EmployeeBean;
import com.vipheyue.bigbusiness.utils.DensityUtil;
import com.vipheyue.bigbusiness.utils.GsonUtils;
import com.vipheyue.bigbusiness.view.CommonAdapter;
import com.vipheyue.bigbusiness.view.RoundedLetterView;
import com.vipheyue.bigbusiness.view.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

public class EmployeeActivity extends Activity {

    @Bind(R.id.rl_menu_left)
    RelativeLayout rl_menu_left;
    @Bind(R.id.tv_Menu_Title)
    TextView tv_Menu_Title;
    @Bind(R.id.iv_menu_right)
    ImageView iv_menu_right;
    @Bind(R.id.rl_menu_right)
    RelativeLayout rl_menu_right;
    @Bind(R.id.pull_refresh_list)
    PullToRefreshListView pull_refresh_list;
    @Bind(R.id.bt_search)
    Button bt_search;
    @Bind(R.id.et_employeeID)
    EditText et_employeeID;
    @Bind(R.id.rlv_employeeNAME)
    RoundedLetterView rlv_employeeNAME;
    @Bind(R.id.tv_employeeCompanyName)
    TextView tv_employeeCompanyName;
    @Bind(R.id.tv_employeeID)
    TextView tv_employeeID;
    @Bind(R.id.tv_employeeCheckInDate)
    TextView tv_employeeCheckInDate;
    @Bind(R.id.tv_employeeWorkTEN)
    TextView tv_employeeWorkTEN;
    @Bind(R.id.ll_employee_item)
    LinearLayout ll_employee_item;
    private List<String> networkImages = new ArrayList<>();
    /*  * 数据中心
    */
    private CommonAdapter<EmployeeBean> mAdapter;
    private List<EmployeeBean> mDatas = new ArrayList<EmployeeBean>();
    private int nowPageNum = 0;
    private int pageSize = 50;
    private String employeeID;
    private EmployeeBean item;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_employee);
        ButterKnife.bind(this);
        tv_Menu_Title.setText("巨业");
        ll_employee_item.setVisibility(View.GONE);
        initRefresh();
        setAdapter();
        initData(0);
    }

    private void initTextDate() {
        BmobQuery<EmployeeBean> query = new BmobQuery<EmployeeBean>();
        if (!TextUtils.isEmpty(employeeID)) {
            query.addWhereEqualTo("employeeID", employeeID);
        }
        query.findObjects(EmployeeActivity.this, new FindListener<EmployeeBean>() {
            @Override
            public void onSuccess(List<EmployeeBean> object) {
                mDatas.addAll(object);
                LogUtils.i("数量:" + mDatas.size());
                item = mDatas.get(0);
                tv_employeeCompanyName.setText(item.getEmployeeCompanyName());
                rlv_employeeNAME.setTitleText(item.getEmployeeNAME());
                if (item.getEmployeeNAME().trim().length() != 2) {
                    rlv_employeeNAME.setTitleSize(DensityUtil.sp2px(EmployeeActivity.this, 10.00f));
                }
                tv_employeeID.setText(item.getEmployeeID());
                tv_employeeCheckInDate.setText(item.getEmployeeCheckInDate());
                tv_employeeWorkTEN.setText(item.getEmployeeWorkTEN());
                ll_employee_item.setVisibility(View.VISIBLE);

            }

            @Override
            public void onError(int code, String msg) {
                if (pull_refresh_list != null) {
                    pull_refresh_list.onRefreshComplete();
                }

            }
        });



}

    private void initRefresh() {
        pull_refresh_list.setMode(PullToRefreshBase.Mode.BOTH);
        pull_refresh_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                String label = DateUtils.formatDateTime(EmployeeActivity.this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
                mDatas.clear();
                nowPageNum = 0;
                initData(nowPageNum);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                String label = DateUtils.formatDateTime(EmployeeActivity.this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
                initData(nowPageNum);
            }

        });
    }

    private void setAdapter() {
        //mdatas 记得加数据进去
        mAdapter = new CommonAdapter<EmployeeBean>(EmployeeActivity.this, mDatas, R.layout.employee_item) {
            @Override
            public void convert(ViewHolder helper, int position, EmployeeBean item) {
                helper.setText(R.id.tv_employeeCompanyName, item.getEmployeeCompanyName());
                helper.setRoundText(R.id.rlv_employeeNAME, item.getEmployeeNAME());
                if (item.getEmployeeNAME().trim().length() != 2) {
                    helper.setRoundTextSize(R.id.rlv_employeeNAME, DensityUtil.sp2px(EmployeeActivity.this, 10.00f));
                }
                helper.setText(R.id.tv_employeeID, item.getEmployeeID());
                helper.setText(R.id.tv_employeeCheckInDate, item.getEmployeeCheckInDate());
                helper.setText(R.id.tv_employeeWorkTEN, item.getEmployeeWorkTEN());
            }
        };
        pull_refresh_list.setOnItemClickListener(new ItemClickListener());
        pull_refresh_list.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
    }

    private void initData(int getNumPage) {
        LogUtils.i("----------------------> initData ");
        BmobQuery<EmployeeBean> query = new BmobQuery<EmployeeBean>();
        query.setLimit(pageSize); // 限制最多10条数据结果作为一页
        query.setSkip(pageSize * getNumPage); // 忽略前10条数据（即第一页数据结果）
        if (!TextUtils.isEmpty(employeeID)) {
            query.addWhereEqualTo("employeeID", employeeID);
        }
        query.findObjects(EmployeeActivity.this, new FindListener<EmployeeBean>() {
            @Override
            public void onSuccess(List<EmployeeBean> object) {
                mDatas.addAll(object);
                LogUtils.i("数量:" + mDatas.size());
                nowPageNum = nowPageNum + 1;
                mAdapter.notifyDataSetChanged();
                // Call onRefreshComplete when the list has been refreshed.
                if (pull_refresh_list != null) {
                    pull_refresh_list.onRefreshComplete();
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (pull_refresh_list != null) {
                    pull_refresh_list.onRefreshComplete();
                }

            }
        });
    }

    @OnClick({R.id.rl_menu_left, R.id.bt_search,R.id.ll_employee_item})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_menu_left:
                finish();
                break;
            case R.id.bt_search:
                employeeID = et_employeeID.getText().toString().trim();
                nowPageNum = 0;
                mDatas.clear();
                initTextDate();
                break;
            case R.id.ll_employee_item:
                String jobStr = GsonUtils.toJson(item);
                Intent intent = new Intent(EmployeeActivity.this, EmployeeDetailActivity.class);
                intent.putExtra("EmployeeStr", jobStr);
                startActivity(intent);
                break;
        }
    }

private final class ItemClickListener implements AdapterView.OnItemClickListener {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//            int REposition=position-1;//加了刷新位置变了
        ListView listView = (ListView) parent;
        EmployeeBean job = (EmployeeBean) listView.getItemAtPosition(position);
        String jobStr = GsonUtils.toJson(job);
        Intent intent = new Intent(EmployeeActivity.this, EmployeeDetailActivity.class);
        intent.putExtra("EmployeeStr", jobStr);
        startActivity(intent);
    }
}

}
